package de.pyranja.util;

import java.util.Date;
import java.util.Random;

public class DateUtils {

	public static long ONE_SEC_AS_MS = 1000L;
	public static long ONE_DAY_AS_MS = 24L * 60L * 60L * ONE_SEC_AS_MS;
	public static long ONE_WEEK_AS_MS = 7L * ONE_DAY_AS_MS;
	public static long ONE_YEAR_AS_MS = 365L * ONE_DAY_AS_MS;

	/**
	 * conversion
	 * @param transformMe
	 * @return
	 */
	public static java.sql.Date toSQLDate(Date transformMe) {
		return new java.sql.Date( transformMe.getTime() );
	}

	/**
	 * adds specified number of days to given date, simple implementation take care !
	 * @param addToMe
	 * @param days
	 * @return
	 */
	public static Date addDaysToDate(Date addToMe, int days) {
		return new Date ( addToMe.getTime() + days * ONE_DAY_AS_MS);
	}

	/**
	 * a date in a specified time period
	 * @param minYearsAgo
	 * @param maxYearsAgo
	 * @return
	 */
	public static Date randomAgoDateByYear( long minYearsAgo, long maxYearsAgo ) {
		return randomAgoDateByYear(new Random(System.currentTimeMillis()), minYearsAgo, maxYearsAgo);
	}

	/**
	 * a date in a specified time period with specified random source
	 * @param rng
	 * @param minYearsAgo
	 * @param maxYearsAgo
	 * @return
	 */
	public static Date randomAgoDateByYear( Random rng, long minYearsAgo, long maxYearsAgo ) {
		long minAgo = minYearsAgo * ONE_YEAR_AS_MS;
		long maxAgo = maxYearsAgo * ONE_YEAR_AS_MS;
		long randomAgo =  ( (long) ( rng.nextDouble() * ( maxAgo - minAgo ) ) + minAgo );
		return new Date(System.currentTimeMillis() - randomAgo);
	}

	public static Date randomForwardDateByWeek( Random rng, long minWeeksForward, long maxWeeksForward ) {
		long minForward = minWeeksForward * ONE_WEEK_AS_MS;
		long maxForward = maxWeeksForward * ONE_WEEK_AS_MS;
		long randomForward = (long) ( ( rng.nextDouble() * ( maxForward - minForward ) ) + minForward );
		return new Date(System.currentTimeMillis() + randomForward);
	}

}
